package com.kang.dao.impl;

import com.kang.dao.Rh_regionDao;
import com.kang.entity.Rh_region;
import com.kang.util.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by 12904 on 2018/7/28.
 */
public class Rh_regionDaoImpl implements Rh_regionDao
{
    @Override
    public Rh_region findByType(int id)
    {
        Rh_region region = null;
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        String sql = "select * from rh_region where id = ?";
        conn = BaseDao.getConnection();
        try {
            stat = conn.prepareStatement(sql);
            stat.setInt(1,id);
            rs = stat.executeQuery();
            while (rs.next()){
                region = new Rh_region();
                region.setId(rs.getInt("id"));
                region.setPid(rs.getInt("pid"));
                region.setName(rs.getString("name"));
                region.setType(rs.getInt("type"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeConnection(conn,stat,rs);
        }

        return region;
    }

    @Override
    public Rh_region findByType()
    {
        Rh_region region = null;
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        String sql = "select id,name from rh_region";
        conn = BaseDao.getConnection();
        try {
            stat = conn.prepareStatement(sql);
            rs = stat.executeQuery();
            while (rs.next()){
                region = new Rh_region();
                region.setId(rs.getInt("id"));
                region.setName(rs.getString("name"));

            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeConnection(conn,stat,rs);
        }

        return region;
    }


}
